package me.zhengjie.modules.housetype.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.housetype.domain.HouseType;
import me.zhengjie.modules.housetype.service.HouseTypeService;
import me.zhengjie.modules.housetype.service.dto.HouseTypeDto;
import me.zhengjie.modules.housetype.service.dto.HouseTypeQueryParam;
import me.zhengjie.modules.location.domain.Location;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.List;
import java.util.Set;

/**
* @author TianI
* @date 2022-01-29
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "房产户型管理")
@RequestMapping("/api/houseType")
public class HouseTypeController {

    private final HouseTypeService houseTypeService;

    @GetMapping
    @Log("查询房产户型")
    @ApiOperation("查询房产户型")
    @PreAuthorize("@el.check('houseType:list')")
    public ResponseEntity query(HouseTypeQueryParam query, Pageable pageable){
        return new ResponseEntity<>(houseTypeService.queryAll(query,pageable),HttpStatus.OK);
    }
    @GetMapping("/getHouseType/{id}")
    public ResponseEntity getHouseType(@PathVariable Long id){
        List<HouseType> houseTypeList = houseTypeService.getHouseType(id);
        return new ResponseEntity<>(houseTypeList,HttpStatus.OK);
    }
    @PostMapping
    @Log("新增房产户型")
    @ApiOperation("新增房产户型")
    @PreAuthorize("@el.check('houseType:add')")
    public ResponseEntity create(@Validated @RequestBody HouseTypeDto resources){
        return new ResponseEntity<>(houseTypeService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改房产户型")
    @ApiOperation("修改房产户型")
    @PreAuthorize("@el.check('houseType:edit')")
    public ResponseEntity update(@Validated @RequestBody HouseTypeDto resources){
        houseTypeService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除房产户型")
    @ApiOperation("删除房产户型")
    @PreAuthorize("@el.check('houseType:del')")
    public ResponseEntity delete(@RequestBody Set<Long> ids) {
        houseTypeService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出房产户型")
    @ApiOperation("导出房产户型")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('houseType:list')")
    public void download(HttpServletResponse response, HouseTypeQueryParam query) throws IOException {
        houseTypeService.download(houseTypeService.queryAll(query), response);
    }*/

}
